<template>
  <router-layout>
    <div class="message">
      <!-- <TitleItem v-if="this.wx==true"></TitleItem> -->
      <div class="vote-ranking van-hairline--bottom">
        <div class="vote-right" v-if="!playerinfo.isvoted" @click="apply">
          <p>投票</p>
        </div>
        <div v-if="playerinfo.isvoted==1" class="vote-right2">
          <p>已投票</p>
        </div>
        <div class="poll">
          <p class="number">{{ weekList.length || 0 }}</p>
          <div class="flex">
            <img src="https://gw.alicdn.com/tfs/TB19VdqOjTpK1RjSZKPXXa3UpXa-40-40.png" alt>
            <p>参赛者</p>
          </div>
        </div>
        <div class="pothunter">
          <p class="number">{{ playerinfo.record_nums || 0 }}</p>
          <div class="flex">
            <img src="https://gw.alicdn.com/tfs/TB1vOhgOgDqK1RjSZSyXXaxEVXa-40-40.png" alt>
            <p>投票数</p>
          </div>
        </div>
      </div>
      <div class="per-mess">
        <p>选手个人信息</p>
      </div>
      <div class="information-box">
        <div class="photo">
          <img :src="playerinfo.file_path" width="100%" alt>
        </div>
        <div class="text-box">
          <p>
            <span>姓名:</span>
            <span>{{ playerinfo.user_name || '暂无' }}</span>
          </p>
          <p>
            <span>编号:</span>
            <span>{{ playerinfo.option_code }}</span>
          </p>
          <p>
            <span>描述:</span>
            <span>{{ playerinfo.option_details }}</span>
          </p>
        </div>
      </div>
    </div>
  </router-layout>
</template>
<script>
import { vote_findById } from "@/api/vote";
import { voteRecord_save } from "@/api/voteRecord";
import TitleItem from "../../components/public/title/title";
export default {
  name: "VoteMess",
  components: { TitleItem },
  data() {
    return {
      datainfo: {},
      weekList: [],
      playerinfo: {},
      listQuery: {}
    };
  },
  methods: {
    apply() {
      let user = JSON.parse(localStorage.getItem("user"));
      this.listQuery = {
        vote_id: this.playerinfo.vote_id,
        vote_optionid: this.playerinfo.id,
        select_nums: this.playerinfo.select_nums,
        user_id: user.id,
        company_id: localStorage.getItem("companyId") - 0
      };
      this.$dialog
        .confirm({
          message: "每个参赛者只能投票一次,您确定要投票吗?"
        })
        .then(() => {
          this.saveObj();
        })
        .catch(() => {});
    },
    async saveObj() {
      // if (this.datainfo.isvote == 1) {
      //   this.$toast("不能重复投票,一天只能投票一次");
      //   return;
      // }
      try {
        let query = Object.assign(this.listQuery);
        let res = await voteRecord_save(query);
        let result = res.data;
        if (result.status == 100) {
          this.$toast.success("投票成功");
          this.initList();
        }
      } catch (error) {
        if (error.data.status == 101) {
          this.$toast.fail(error.data.msg || "不能重复投票,一天只能投票一次");
        }
      }
    },
    //获取数据
    async initList() {
      let query = Object.assign(
        {},
        { id: this.$route.query.id },
        { company_id: localStorage.getItem("companyId") - 0 }
      );
      let res = await vote_findById(query);
      let result = res.data;
      if (result.status == 100) {
        this.datainfo = result.data;
        this.weekList = result.data.voteOptions;
      }
    }
  },
  created() {
    this.initList();
    this.playerinfo = this.$route.query.content;
  }
};
</script>
<style lang="scss" scoped>
@import "VoteMess";
</style>
